package com.example.test1.fragment;


import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.test1.Main3Activity;
import com.example.test1.R;
import com.example.test1.adapter.RlvAdapter;
import com.example.test1.bean.Lists;
import com.example.test1.bean.Paraments;
import com.example.test1.http.Api;
import com.example.test1.moudle.Moudle;
import com.example.test1.presenter.Presenter;
import com.example.test1.view.Main_View;
import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * A simple {@link Fragment} subclass.
 */
@SuppressLint("ValidFragment")
public class ShowFragment extends Fragment implements Main_View {


    private  Main3Activity mContext;
    @BindView(R.id.rlv)
    XRecyclerView mRlv;
    private int cursor = 0;
    private String mId;
    private View view;
    private Unbinder unbinder;
    RlvAdapter mRlvAdapter;

    public ShowFragment(String channelId, Main3Activity context) {
        // Required empty public constructor
        this.mId = channelId;
        this.mContext=context;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_show, container, false);
        data();
        unbinder = ButterKnife.bind(this, inflate);
        aaa();
        return inflate;
    }

    private void aaa() {
         final ArrayList<Lists.DataBean.NewListBean> newList = new ArrayList<>();
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        mRlv.setLayoutManager(layoutManager);
        mRlvAdapter = new RlvAdapter(newList,getContext());
        mRlv.setAdapter(mRlvAdapter);
        mRlv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                newList.clear();
                data();
                mRlv.refreshComplete();
            }

            @Override
            public void onLoadMore() {
                cursor++;
                data();
            }
        });
        //点击监听
        mRlvAdapter.setOnItemClickListener(new RlvAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View v, int position) {
                UMImage image = new UMImage(mContext, R.mipmap.ic_launcher);//网络图片
                new ShareAction(mContext).withText("hello").withMedia(image).setDisplayList(SHARE_MEDIA.SINA, SHARE_MEDIA.QQ, SHARE_MEDIA.WEIXIN)
                        .setCallback(new UMShareListener() {
                            @Override
                            public void onStart(SHARE_MEDIA share_media) {

                            }

                            @Override
                            public void onResult(SHARE_MEDIA share_media) {
                                Log.e("duan", "onResult");
                            }

                            @Override
                            public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                                Log.e("duan", throwable.getMessage());
                            }

                            @Override
                            public void onCancel(SHARE_MEDIA share_media) {

                            }
                        }).open();

            }
        });
    }

    private void data() {
        Presenter presenter = new Presenter(new Moudle(), this);
        Paraments paraments = new Paraments();
        paraments.setChannelId(mId);
        paraments.setCursor(cursor+"");
        Gson gson = new Gson();
        String s = gson.toJson(paraments);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json,charset-UTF-8"), s);
        presenter.getData("downListNews",requestBody,Api.LIST);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();

    }
    @Override
    public void ShowSuccess(Object o, Api api) {
        String s= (String) o;
        Log.e("duan", "ShowSuccess: ===============sd=>"+s);
        Gson gson = new Gson();
        Lists lists = gson.fromJson(s, Lists.class);
        List<Lists.DataBean.NewListBean> newList = lists.getData().getNewList();
        Log.e("duan", "newList: "+newList);
        if (newList.size()>0){
            mRlvAdapter.addData(newList);
        }
        mRlv.loadMoreComplete();
    }

    @Override
    public void ShowError(String error) {

    }


}
